package edu.zut.main.socket;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class URLDown {

    public static void main(String[] args) throws IOException {

        //网络资源链接
        String url = "https://blog.csdn.net/pashanhu6402/article/details/96428887";
        //基于url链接构建url对象
        URL articleUrl = new URL(url);
//        String host = articleUrl.getHost();
//        System.out.println(host);
//        String path = articleUrl.getPath();
//        System.out.println(path);
//        System.out.println(articleUrl.getPort());
        //打开这个链接
        URLConnection connection = articleUrl.openConnection();
        BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
        //实际中，保存文件的时候，文件名随机生成
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("./article.html")));

        byte[] bytes = new byte[521];
        int len;
        while ((len = bis.read(bytes)) != -1){
            bos.write(bytes,0,len);
        }
        bos.flush();
        bos.close();
        bis.close();
    }

}
